import { getToken, setToken, removeToken } from '@/utils/auth'
import { login } from '@/api/login/index'
import { getUserPerm } from '@/api/permissions'
import { getUserInfo } from '@/api/user'
export default {
  namespaced: true,
  state: {
    token: getToken(),
    userInfo: {},
    UserPerm: {}
  },
  mutations: {
    set_Token(state, token) {
      state.token = token
      setToken(token)
    },
    remove_Token(state) {
      state.token = ''
      removeToken()
    },
    set_UserInfo(state, user) {
      state.userInfo = user
    },
    removeUserInfo(state) {
      state.userInfo = {}
    },
    set_UserPerm(state, data) {
      state.UserPerm = data
    }
  },
  actions: {
    // 登录和用户信息都在这
    async login(store, loginForm) {
      const res = await login(loginForm)
      // console.log(res)
      store.commit('set_Token', res.data.jwt)
      // store.commit('set_UserInfo', res.data.user)
    },
    // 获取用户权限
    // async getUserPerm(store) {
    //   const zxc = await getUserPerm()
    //   console.log(33, zxc.data)
    //   store.commit('set_UserPerm', zxc.data.menus)
    //   // const res = await getUserInfo()
    //   // console.log('res', res)
    // },
    // 获取用户信息
    async getUserInfo(store) {
      const res = await getUserInfo()
      // console.log('res', res)
      store.commit('set_UserInfo', res.data)
      const zxc = await getUserPerm()
      // console.log(33, zxc.data)
      store.commit('set_UserPerm', zxc.data.menus)
    },
    // 退出
    async logout(store) {
      store.commit('remove_Token')
      store.commit('removeUserInfo')
    }

  }
}
